﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
public partial class Shared_register : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       // Pan.Common.MessageBox.Show(this, "邀请者ID：" + Pan.Cookie.GetCookie("reuid"));
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Response.Clear();
        //Response.Write("<script language=javascript>window.alert('您没有权限进入本页！\\n请登录或与管理员联系！');history.back();</script>");
        //Response.End();
        string strEmail = email.Value.Trim();
        string strPass = pass.Value;

        try
        {

            #region memebership 创建状态
            //MembershipCreateStatus iStatus;

            //Membership.CreateUser(strEmail, strPass, strEmail, "无","无",true, out   iStatus);
            //switch (iStatus)
            //{
            //    case MembershipCreateStatus.Success:
            //        if (Request.QueryString["ReturnUrl "] != null)
            //        {
            //            FormsAuthentication.RedirectFromLoginPage(strEmail, false);
            //        }
            //        else
            //        {
            //            FormsAuthentication.SetAuthCookie(strEmail, false);
            //            Response.Redirect("~/default.aspx ");
            //        }
            //        Pan.Common.MessageBox.Show(this,"新用户创建成功！ ");
            //        break;
            //    case MembershipCreateStatus.InvalidUserName:
            //        Pan.Common.MessageBox.Show(this, "无效的用户名。 ");
            //        break;
            //    case MembershipCreateStatus.DuplicateEmail:
            //        Pan.Common.MessageBox.Show(this, "重复的电子邮件地址。 ");
            //        break;
            //    case MembershipCreateStatus.InvalidQuestion:
            //        Pan.Common.MessageBox.Show(this, "无效的密码提示问题。 ");
            //        break;
            //    case MembershipCreateStatus.DuplicateUserName:
            //        Pan.Common.MessageBox.Show(this, "该用户名已注册，请更换用户名。 ");
            //        break;
            //    case MembershipCreateStatus.InvalidAnswer:
            //        Pan.Common.MessageBox.Show(this, "无效的密码答案。 ");
            //        break;
            //    case MembershipCreateStatus.InvalidEmail:
            //        Pan.Common.MessageBox.Show(this, "无效的电子邮件地址。 ");
            //        break;
            //    case MembershipCreateStatus.InvalidPassword:
            //        Pan.Common.MessageBox.Show(this, "密码须为７位，并且有１个特殊符号。 ");
            //        break;
            //    default:
            //        Pan.Common.MessageBox.Show(this, "出现未知错误！请联系网站管理员。 ");
            //        break;
            //}

            #endregion
            
            MembershipUserCollection col = Membership.FindUsersByName(strEmail);
            if (col.Count != 0)
            {
                Pan.Common.MessageBox.Show(this,"此Email已经被使用，请重试!");
                return;
            }
            MembershipUser user = Membership.CreateUser(strEmail, strPass, strEmail);
            Roles.AddUserToRole(strEmail, "Users");
            Tuan.Model.UserInfo info = new Tuan.Model.UserInfo();
            info.Email = email.Value.Trim();
            info.TokenAmount = 5;
            info.CityID =int.Parse(Pan.Sys_Config.GetCurrentCityID());
            info.IsPass = false;

            if (!string.IsNullOrEmpty(Pan.Cookie.GetCookie("reuid")))
                info.ParentID = int.Parse(Pan.Cookie.GetCookie("reuid"));//邀请人ID

            Tuan.BLL.UserInfo obj = new Tuan.BLL.UserInfo();
            int userid = obj.Add(info);

            //奖励内测注册
            
            if (userid > 0)
            {
                Tuan.Model.Event eventobj = new Tuan.Model.Event();
                eventobj.Amount = 5;
                eventobj.EventName = "注册即送5元!";
                eventobj.UserID = userid;
                
                (new Tuan.BLL.Event()).Add(eventobj);
            }
            if (Context.Request["ReturnUrl"] != null)
            {
                //Response.Redirect();
                if (Context.Request["ReturnUrl"].Contains("pages/default.aspx"))
                    //Response.Redirect("/pages/usercenter/ChangeMyData.aspx");
                      Pan.Common.MessageBox.ShowAndRedirect(this, "注册成功，你已经获得5元现金券奖励！", "/pages/usercenter/ChangeMyData.aspx"); 
                else
                   // Response.Redirect(Context.Request["ReturnUrl"]);
                    Pan.Common.MessageBox.ShowAndRedirect(this, "注册成功，你已经获得5元现金券奖励！", Context.Request["ReturnUrl"]); 
            }
            else
            {
                //Response.Write("/pages/usercenter/MyorderList.aspx");
                Pan.Common.MessageBox.ShowAndRedirect(this, "注册成功，你已经获得5元现金券奖励！", "/pages/usercenter/MyorderList.aspx"); 
            }
                      
        }
        catch (Exception ex)
        {
            Pan.Common.MessageBox.Show(this, ex.Message.ToString());

        }



    }
}
